﻿using System;
using System.Collections.Generic;
using System.Linq;
using TamoCRM.Core.Caching;
using TamoCRM.Domain.Channels;
using TamoCRM.Domain.EducationLevels;
using TamoCRM.Domain.Locations;
using TamoCRM.Domain.Majors;
using TamoCRM.Domain.Schools;
using TamoCRM.Services.Channels;
using TamoCRM.Services.Contacts;
using TamoCRM.Services.EducationLevels;
using TamoCRM.Services.Locations;
using TamoCRM.Services.Majors;
using TamoCRM.Services.Phones;
using TamoCRM.Services.Schools; 

namespace TamoCRM.ImportExcel.Library
{
    public static class StaticData
    {
        private static readonly Dictionary<string, SchoolInfo> _schools = new Dictionary<string, SchoolInfo>();
        private static readonly Dictionary<string, MajorInfo> _majors = new Dictionary<string, MajorInfo>();
        private static readonly Dictionary<string, ChannelInfo> _channels = new Dictionary<string, ChannelInfo>(); 
        private static readonly Dictionary<string, LocationInfo>  _locations = new Dictionary<string, LocationInfo>();
        private static readonly Dictionary<string, EducationLevelInfo> _educationLevels = new Dictionary<string, EducationLevelInfo>();

        public static string GetAllMajor()
        {
            return _majors
                .Select(c => c.Value)
                .Where(c => c.MajorId != 1)
                .Select(c => c.MajorId.ToString())
                .Aggregate((total, curent) => total + "|" + curent);
        }

        public static int GetEducationLevel(string name)
        {
            if (string.IsNullOrEmpty(name)) return 1;
            return _educationLevels.ContainsKey(name) ? _educationLevels[name].EducationLevelId : 1;
        }
        public static int GetLocationId(string name)
        {
            if (string.IsNullOrEmpty(name)) return 1;
            return _locations.ContainsKey(name) ? _locations[name].LocationId : 1;
        }
        public static int GetSchoolId(string code)
        {
            if (string.IsNullOrEmpty(code)) return 1;
            return _schools.ContainsKey(code) ? _schools[code].SchoolId : 1;
        }
        public static int GetMajorId(string code)
        {
            if (string.IsNullOrEmpty(code)) return 1;
            return _majors.ContainsKey(code) ? _majors[code].MajorId : 1;
        }
        public static int GetChannelId(string code)
        {
            if (string.IsNullOrEmpty(code)) return 1;
            return _channels.ContainsKey(code) ? _channels[code].ChannelId : 1;
        }
        // haihm #002
        public static int GetChannelId(ChannelInfo channel, int defaultId)
        {
            if (string.IsNullOrEmpty(channel.Name)) return defaultId;
            if (_channels.ContainsKey(channel.Name))
                return _channels[channel.Name].ChannelId;
            // thêm channel mới vào DB
            channel.ChannelId = ChannelRepository.Create(channel);
            // bổ sung vào _channels
            _channels.Add(channel.Name, channel);
            return channel.ChannelId;
        }
        public static void LoadData()
        {
            var schools = SchoolRepository.GetAll();
            foreach (var info in schools
                .Where(c => !string.IsNullOrEmpty(c.Code))
                .Where(c => !_schools.ContainsKey(c.Code)))
                _schools.Add(info.Code, info);

            var majors = MajorRepository.GetAll();
            foreach (var info in majors
                .Where(info => !string.IsNullOrEmpty(info.Name))
                .Where(c => !_majors.ContainsKey(c.Name)))
                _majors.Add(info.Name, info);

            var channels = ChannelRepository.GetAll();
            foreach (var info in channels
                .Where(c => !string.IsNullOrEmpty(c.Name))
                .Where(c => !_channels.ContainsKey(c.Name)))
                _channels.Add(info.Name, info);
            
            var locations = LocationRepository.GetAll();
            foreach (var info in locations
                .Where(c => !string.IsNullOrEmpty(c.Name))
                .Where(c => !_locations.ContainsKey(c.Name)))
                _locations.Add(info.Name, info);

            var educationLevels = EducationLevelRepository.GetAll();
            foreach (var info in educationLevels
                .Where(c => !string.IsNullOrEmpty(c.Name))
                .Where(c => !_educationLevels.ContainsKey(c.Name)))
                _educationLevels.Add(info.Name, info);
        }
        public static void LoadToRedis()
        {
            var lstPhones = PhoneRepository.GetAll_ForRedis();
            foreach (var phone in lstPhones.Where(c => !string.IsNullOrEmpty(c.PhoneNumber)))
                CachingProvider.Instance().Set(phone.PhoneNumber, phone.ContactId.ToString());
            
            var lstContacts = ContactRepository.GetAll_ForRedis();
            foreach (var info in lstContacts.Where(c => !string.IsNullOrEmpty(c.Email)))
                CachingProvider.Instance().Set(info.Email, info.Id.ToString());
            foreach (var info in lstContacts.Where(c => !string.IsNullOrEmpty(c.Email2)))
                CachingProvider.Instance().Set(info.Email2, info.Id.ToString());
        }
        public static void ClearData()
        {
            _majors.Clear();
            _schools.Clear();
            _channels.Clear();
            _locations.Clear();
            _educationLevels.Clear();
        }
        public static void ReLoadData()
        {
            ClearData();
            LoadData();
        }

        public static ChannelInfo ParseChannel(string input)
        {
            var info = new ChannelInfo();
            if (!string.IsNullOrEmpty(input)) input = input.Trim();
            if (!string.IsNullOrEmpty(input))
            {
                var index = input.IndexOf(".");
                if (index > -1)
                {
                    info.Code = input.Substring(0, index);
                    info.Name = input.Substring(0);
                    info.Description = string.Empty;
                    info.CreatedBy = 0; info.CreatedDate = DateTime.Now;
                    info.ChangedBy = 0; info.ChangedDate = DateTime.Now;
                }
            }
            return info;
        }

    }

}
